package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;

import cn.wolfcode.mapper.EmployeeMapper;

import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void save(Employee employee, Long[] ids) {
        //检查参数合法性
        //假设业务很复杂
        employeeMapper.insert(employee);
        //保存员工后才有id
        //关联关系,把数据插入到中间表
        if (ids != null && ids.length>0){
            for (Long rid: ids) {
                //rid是角色的id
                employeeMapper.insertRelation(employee.getId(),rid);
            }
        }
    }

    @Override
    public void delete(Long id) {
        //删除员工也要删除关系
        employeeMapper.deleteRelation(id);
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);
        //先删除关系
        employeeMapper.deleteRelation(employee.getId());
        //在,关联关系,把数据插入到中间表
        if (ids != null && ids.length>0){
            for (Long rid: ids) {
                //rid是角色的id
                employeeMapper.insertRelation(employee.getId(),rid);
            }
        }
    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }


    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        /*int count = employeeMapper.selectForCount(qo);
        if (count == 0) {
            return new PageResult<>(qo.getCurrentPage(), qo.getPageSize()
                    , Collections.emptyList(), count);
        }
        //因为当删除最后一页最后一条数据时,页面任然返回的是,最后一页的,所以会导致页面无法显示,此时需要修改
        //判断实际页是否大于当前页数,是则重新赋值
        int totalPage = count % qo.getPageSize() == 0 ? count / qo.getPageSize() : count / qo.getPageSize() + 1;
        if (qo.getCurrentPage() > totalPage){
            qo.setCurrentPage(totalPage);
        }
        List<Employee> employees = employeeMapper.selectForList(qo);
        return new PageResult<>(qo.getCurrentPage(), qo.getPageSize(), employees, count);*/

        //工具的分页插件
        //可以点startPage,再点下载源码,就能看见中文注释
        //业务层控制器要跟着修改
        //开始分页(需要传入当前页,传入每一页显示数量),下一句
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        //sql不要写limit,插件会自动在sql语句后面加上limit语句
        List<Employee> employees = employeeMapper.selectForList(qo);
        return new PageInfo<>(employees);

    }

    @Override
    public Employee login(String username, String password) {
        //方法一,别人写好的方法
        if (!StringUtils.hasText(username)){
            throw new RuntimeException("用户名不能为空");
        }
        //方法二,自己写的
       /* if (username==null||username.equals("")){
            throw new RuntimeException("用户名不能为空");
        }*/
        if (password==null||password.equals("")){
            throw new RuntimeException("密码不能为空");
        }
        //查询数据库中是否有匹配的数据
        Employee employee = employeeMapper.selectByUsernameAndPassword(username,password);
        if(employee==null){
            //通知调用者出现异常
            throw new RuntimeException("账号或密码错误");
        }
        return employee;
    }


}
